﻿using AutoMapper;
using MediatR;
using Traceability.Commont.Domains;
using Traceability.Commont.DTOs.CommontFiles;
using Traceability.Commont.Infrastructures.Interfaces;

namespace Traceability.Api.Applications.Userss
{
    public class UserURLHander : IRequestHandler<UsaerURLCommand, Resqults<Permissions>>
    {
        private readonly IBateRepository<Users> user;
        private readonly IBateRepository<Roles> roles;
        private readonly IBateRepository<UserRoles> userRoles;
        private readonly IBateRepository<Permissions> permissions;
        private readonly IBateRepository<RolesPermissiont> rolesPermissions;
        private readonly IMapper mapper;

        public UserURLHander(IBateRepository<Users> user,
                             IBateRepository<Roles> roles,
                             IBateRepository<UserRoles> userRoles,
                             IBateRepository<Permissions> permissions,
                             IBateRepository<RolesPermissiont> rolesPermissions,
                             IMapper mapper)
        {
            this.user = user;
            this.roles = roles;
            this.userRoles = userRoles;
            this.permissions = permissions;
            this.rolesPermissions = rolesPermissions;
            this.mapper = mapper;
        }

        public async Task<Resqults<Permissions>> Handle(UsaerURLCommand request, CancellationToken cancellationToken)
        {
            try
            {
                Resqults<Permissions> res = new Resqults<Permissions>() { Code = 0 };
                var list = (from a in user.GetAll()
                           join b in userRoles.GetAll() on a.UserId equals b.UserId
                           join c in roles.GetAll() on b.RolesId equals c.RolesId
                           join d in rolesPermissions.GetAll() on c.RolesId equals d.RolesId
                           join e in permissions.GetAll() on d.PermissionsId equals e.PermissionsId
                           where a.UserId == request.UserId
                           select e).ToList();
                res.Date = list;

                return res;
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
